home *** CD-ROM | disk | FTP | other *** search
- Path: news.Arizona.EDU!seds!chrisl
- From: chrisl@SEDS.LPL.Arizona.EDU (Chris Lewicki)
- Newsgroups: comp.lang.c
- Subject: 12 bit datatype in binary file - misaligned
- Date: 22 Mar 1996 09:13:13 GMT
- Organization: The University of Arizona
- Message-ID: <4itqv9$kcq@news.ccit.arizona.edu>
- NNTP-Posting-Host: seds.lpl.arizona.edu
-
- Hello All,
- I've been scouring the net and the bookstores for the last few
- days trying to figure out how to read a 12-bit data length out of a
- binary file.
- Here's the setup: I have a data file that has all lengths of
- fields in it. There are 1, 2, 3, 4, 6, 8, 12, and 16 bit data types
- all packed in very efficiently. I've figured out that you can define
- a structure:
-
- struct BitData {
- unsigned onebit : 1;
- unsigned anotheronebit : 1;
- unsigned fourbit : 4;
- unsigned twobit : 2;
- unsigned : 4; /* 4 bits padding */
- unsigned char eightbit;
- unsigned twelvebit : 12;
- unsigned sixteenbit;
- };
-
- struct BitData bitdata;
-
- Using this I could read in a file formatted this way with a simple
- fread(&bitdata, ...) but I find that I encounter problems when I get
- to the 12 bit data types. It will read in the first 12 bit variable
- encountered, but everything after that is misaligned. I've searched
- the world over, but I can't find a thing on this topic.
- I understand that this is implementation dependent, and I am
- using gcc 2.7.2 (and/or SUNWspro cc) on a Sparc 10 running Solaris
- 2.5. I've tried the Sun cc compiled using the -misalign compile flag,
- but that doesn't work either.
- Will I be forced to read in binary blocks and parse them out?
- Please no!!! I'd much rather do a single fread, that a few hundred bit
- shifiting operations!
-
- I just checked this group for the first time in a while, and saw quite
- a few things on bit operations, but I didn't see anything in the FAQ
- relating to my problem. Anyway, I don't hang out here too much, so
- please cc: me if you happen to know the fix!
-
- Eternally indebted...
-
- --
- ------------------------------------------------------------------------------
- Christopher A. Lewicki KC7NYV 520.703.5542 Maintainer of SEDS.LPL.Arizona.EDU
- Chair, Students for the Exploration and Development of Space (SEDS-USA)
-